package com.pic.mgt.haishin;

import java.io.File;
import java.io.IOException;

import com.pic.mgt.haishin.converter.HaishinBusinessUnitConverter;
import com.pic.mgt.haishin.unit.AppointedHaishinBusinessUnit;
import com.pic.mgt.haishin.unit.GeneralHaishinBusinessUnit;
import com.pic.mgt.log.IgxLog;
import com.pic.mgt.monitor.BusinessUnitFactory;
import com.pic.mgt.monitor.RedundantConfirmationFileException;
import com.pic.mgt.monitor.UnitConverter;
import com.pic.mgt.monitor.WrongUnitFormatException;
import com.pic.mgt.unit.Unit;

public class HaishinBusinessUnitFactory extends BusinessUnitFactory {	
	private UnitConverter appointedHaishinBusinessUnitConverter;
	private UnitConverter generalHaishinBusinessUnitConverter;
	
	public HaishinBusinessUnitFactory(String appintedHaishinTypeStr,
			   						  String generalHaishinTypeStr,
			   						  String realSyushinTypeStr,
			   						  String batchSyushinTypeStr) {
		super(appintedHaishinTypeStr,
			  generalHaishinTypeStr,
			  realSyushinTypeStr,
			  batchSyushinTypeStr);
	}	
	
	@Override
	public Unit createUnit(File confirmationFile)
											throws IOException,
												   WrongUnitFormatException,
												   RedundantConfirmationFileException {
		Unit haishinBusinessUnit = null;	
		
		try {
			// 取得業務型態
			// throws WrongUnitFormatException 
			String haishinTypeString = (String) super.businessTypeParser
					.parse(confirmationFile.getName());
			if (haishinTypeString.equals(APPINTED_HAISHIN)) {
				// 指示配信

				// ********************** Log **********************
				if (IgxLog.messageDetailsLog.isInfoMode()) {
					IgxLog.messageDetailsLog
							.info(this, "Creating appinted haishin business unit for confirmation file '{}'",
									confirmationFile.getName());
				}
				// ********************** Log **********************

				/*
				 *  throws NoMatchHaishinTypeException,
				 *         IOException,
				 *         WrongUnitFormatException, 
				 *         RedundantConfirmationFileOfBusinessException
				 */
				haishinBusinessUnit = this.appointedHaishinBusinessUnitConverter
						.parseToUnit(confirmationFile,
								new AppointedHaishinBusinessUnit(APPINTED_HAISHIN));

				// ********************** Log **********************
				if (IgxLog.messageDetailsLog.isInfoMode()) {
					IgxLog.messageDetailsLog
							.info(this, "Creating appinted haishin business unit for confirmation file '{}' completed.",
									confirmationFile.getName());
				}
				// ********************** Log **********************
			}
			else if (haishinTypeString.equals(GENERAL_HAISHIN)) {
				// 一般配信

				// ********************** Log **********************
				if (IgxLog.messageDetailsLog.isInfoMode()) {
					IgxLog.messageDetailsLog
							.info(this, "Creating general haishin business unit for confirmation file '{}'",
									confirmationFile.getName());
				}
				// ********************** Log **********************

				/*
				 *  throws NoMatchHaishinTypeException,
				 *         IOException,
				 *         WrongUnitFormatException,
				 *         RedundantConfirmationFileOfBusinessException
				 */
				haishinBusinessUnit = this.generalHaishinBusinessUnitConverter
						.parseToUnit(confirmationFile,
								new GeneralHaishinBusinessUnit(GENERAL_HAISHIN));

				// ********************** Log **********************
				if (IgxLog.messageDetailsLog.isInfoMode()) {
					IgxLog.messageDetailsLog
							.info(this, "Creating general haishin business unit for confirmation file '{}' completed.",
									confirmationFile.getName());
				}
				// ********************** Log **********************
			}
			else {
				// 找不到對應的業務'型態'
				throw new WrongUnitFormatException(
						"Can't find match haishin type '"
								+ haishinTypeString
								+ "' to create haishin business unit for confirmation file '"
								+ confirmationFile.getName() + "'.");
			}
		} catch (WrongUnitFormatException e1) {
			// ********************** Log **********************
			IgxLog.messageDetailsLog.error(this, e1.getMessage());
			// ********************** Log **********************
			
			throw e1;
		} catch (IOException e2) {
			// ********************** Log **********************
			if (IgxLog.messageDetailsLog.isDebugMode()) {
				IgxLog.messageDetailsLog.debug(this, e2.getMessage());
			}
			// ********************** Log **********************
			
			throw e2;
		}
		
		return haishinBusinessUnit;
	}
	
	/**
	 * 判斷是否為有效的配信業務型態
	 * @param haishinBusinessTypeStr
	 * @return
	 */
	public static boolean isEffectiveHaishinBusinessType(String haishinBusinessTypeStr) {
		if (haishinBusinessTypeStr.equals(APPINTED_HAISHIN)||
			haishinBusinessTypeStr.equals(GENERAL_HAISHIN)) {
			return true;
		}
		else {
			return false;
		}
	}
	
	// --- getter and setter ---

	public void setAppointedHaishinBusinessUnitConverter(
			HaishinBusinessUnitConverter appointedHaishinBusinessParser) {
		this.appointedHaishinBusinessUnitConverter = appointedHaishinBusinessParser;
	}

	public void setGeneralHaishinBusinessUnitConverter(
			HaishinBusinessUnitConverter generalHaishinBusinessParser) {
		this.generalHaishinBusinessUnitConverter = generalHaishinBusinessParser;
	}

}
